package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.Staff;
import com.api.shopapi.entity.Zone;
import com.api.shopapi.entity.dto.ZoneDTO;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.ZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

//组织

@RestController
@RequestMapping("Zone")
public class ZoneController extends BaseController {

    @Autowired
    @Qualifier("ZoneServiceImplOne")
    private ZoneService zoneService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    //列表
    @RequestMapping(value = "List",method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/Zone/List");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<Zone> listZone = zoneService.getListByParentZoneCodeGovernmentCode(emptyUUID,staff.getGovernmentCode());
            List<ZoneDTO> listZoneDTO = new ArrayList<ZoneDTO>();
            for (Zone zone:listZone) {
                ZoneDTO zoneDTO = mapper.map(zone, ZoneDTO.class);
                listZoneDTO.add(zoneDTO);
            }

            for (ZoneDTO zoneDTO:listZoneDTO) {
                List<Zone> listZone2 = zoneService.getListByParentZoneCode(zoneDTO.getZoneCode());
                List<ZoneDTO> listZoneDTO2 = new ArrayList<ZoneDTO>();
                for (Zone zone:listZone2) {
                    ZoneDTO zoneDTO2 = mapper.map(zone, ZoneDTO.class);
                    listZoneDTO2.add(zoneDTO2);
                }

                for (ZoneDTO zoneDTO2:listZoneDTO2) {
                    List<Zone> listZone3 = zoneService.getListByParentZoneCode(zoneDTO2.getZoneCode());
                    List<ZoneDTO> listZoneDTO3 = new ArrayList<ZoneDTO>();
                    for (Zone zone:listZone3) {
                        ZoneDTO zoneDTO3 = mapper.map(zone, ZoneDTO.class);
                        listZoneDTO3.add(zoneDTO3);
                    }

                    for (ZoneDTO zoneDTO3:listZoneDTO3) {
                        List<Zone> listZone4 = zoneService.getListByParentZoneCode(zoneDTO3.getZoneCode());
                        List<ZoneDTO> listZoneDTO4 = new ArrayList<ZoneDTO>();
                        for (Zone zone:listZone4) {
                            ZoneDTO zoneDTO4 = mapper.map(zone, ZoneDTO.class);
                            listZoneDTO4.add(zoneDTO4);
                        }

                        for (ZoneDTO zoneDTO4:listZoneDTO4) {
                            List<Zone> listZone5 = zoneService.getListByParentZoneCode(zoneDTO4.getZoneCode());
                            List<ZoneDTO> listZoneDTO5 = new ArrayList<ZoneDTO>();
                            for (Zone zone:listZone5) {
                                ZoneDTO zoneDTO5 = mapper.map(zone, ZoneDTO.class);
                                listZoneDTO5.add(zoneDTO5);
                            }


                            zoneDTO4.setListZoneDTO(listZoneDTO5);
                        }

                        zoneDTO3.setListZoneDTO(listZoneDTO4);
                    }

                    zoneDTO2.setListZoneDTO(listZoneDTO3);
                }

                zoneDTO.setListZoneDTO(listZoneDTO2);
            }


            return success(listZoneDTO);

        }catch (Exception e){
            insertError("/Zone/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //列表(服务人员)
    @RequestMapping(value = "ListForStaffServe",method = RequestMethod.POST)
    public MyResult ListForStaffServe(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/Zone/ListForStaffServe");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            Staff staff1 =staffService.getByStaffCode(staff.getParentStaffCode());


            String parentZoneCode = staff1.getZoneCode();
            if(staff1.getZoneCode() == null){
                parentZoneCode = emptyUUID;
            }

            List<Zone> listZone = zoneService.getListByParentZoneCodeGovernmentCode(parentZoneCode,staff1.getGovernmentCode());
            List<ZoneDTO> listZoneDTO = new ArrayList<ZoneDTO>();
            for (Zone zone:listZone) {
                ZoneDTO zoneDTO = mapper.map(zone, ZoneDTO.class);
                listZoneDTO.add(zoneDTO);
            }

            for (ZoneDTO zoneDTO:listZoneDTO) {
                List<Zone> listZone2 = zoneService.getListByParentZoneCode(zoneDTO.getZoneCode());
                List<ZoneDTO> listZoneDTO2 = new ArrayList<ZoneDTO>();
                for (Zone zone:listZone2) {
                    ZoneDTO zoneDTO2 = mapper.map(zone, ZoneDTO.class);
                    listZoneDTO2.add(zoneDTO2);
                }

                for (ZoneDTO zoneDTO2:listZoneDTO2) {
                    List<Zone> listZone3 = zoneService.getListByParentZoneCode(zoneDTO2.getZoneCode());
                    List<ZoneDTO> listZoneDTO3 = new ArrayList<ZoneDTO>();
                    for (Zone zone:listZone3) {
                        ZoneDTO zoneDTO3 = mapper.map(zone, ZoneDTO.class);
                        listZoneDTO3.add(zoneDTO3);
                    }

                    for (ZoneDTO zoneDTO3:listZoneDTO3) {
                        List<Zone> listZone4 = zoneService.getListByParentZoneCode(zoneDTO3.getZoneCode());
                        List<ZoneDTO> listZoneDTO4 = new ArrayList<ZoneDTO>();
                        for (Zone zone:listZone4) {
                            ZoneDTO zoneDTO4 = mapper.map(zone, ZoneDTO.class);
                            listZoneDTO4.add(zoneDTO4);
                        }

                        for (ZoneDTO zoneDTO4:listZoneDTO4) {
                            List<Zone> listZone5 = zoneService.getListByParentZoneCode(zoneDTO4.getZoneCode());
                            List<ZoneDTO> listZoneDTO5 = new ArrayList<ZoneDTO>();
                            for (Zone zone:listZone5) {
                                ZoneDTO zoneDTO5 = mapper.map(zone, ZoneDTO.class);
                                listZoneDTO5.add(zoneDTO5);
                            }


                            zoneDTO4.setListZoneDTO(listZoneDTO5);
                        }

                        zoneDTO3.setListZoneDTO(listZoneDTO4);
                    }

                    zoneDTO2.setListZoneDTO(listZoneDTO3);
                }

                zoneDTO.setListZoneDTO(listZoneDTO2);
            }

            return success(listZoneDTO);

        }catch (Exception e){
            insertError("/Zone/ListForStaffServe",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //列表(for政府，组织)
    @RequestMapping(value = "ListForStaff",method = RequestMethod.POST)
    public MyResult ListForStaff(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/Zone/ListForStaff");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            String parentZoneCode = staff.getZoneCode();

            if(staff.getZoneCode() == null || staff.getZoneCode().equals("")){
                parentZoneCode = emptyUUID;
            }

            List<Zone> listZone=null;
//          根据zoneCode的值来确定是用子类开始查询,还是直接用zongCode查询
            if (staff.getZoneCode()==null || staff.getZoneCode().equals("")){
                listZone = zoneService.getListByParentZoneCodeGovernmentCode(parentZoneCode,staff.getGovernmentCode());
            }else {
                listZone = zoneService.getListByParentZoneCodeGovernmentCode1(staff.getZoneCode(),staff.getGovernmentCode());
            }

            List<ZoneDTO> listZoneDTO = new ArrayList<ZoneDTO>();
            for (Zone zone:listZone) {
                ZoneDTO zoneDTO = mapper.map(zone, ZoneDTO.class);
                listZoneDTO.add(zoneDTO);
            }

            for (ZoneDTO zoneDTO:listZoneDTO) {
                List<Zone> listZone2 = zoneService.getListByParentZoneCode(zoneDTO.getZoneCode());
                List<ZoneDTO> listZoneDTO2 = new ArrayList<ZoneDTO>();
                for (Zone zone:listZone2) {
                    ZoneDTO zoneDTO2 = mapper.map(zone, ZoneDTO.class);
                    listZoneDTO2.add(zoneDTO2);
                }

                for (ZoneDTO zoneDTO2:listZoneDTO2) {
                    List<Zone> listZone3 = zoneService.getListByParentZoneCode(zoneDTO2.getZoneCode());
                    List<ZoneDTO> listZoneDTO3 = new ArrayList<ZoneDTO>();
                    for (Zone zone:listZone3) {
                        ZoneDTO zoneDTO3 = mapper.map(zone, ZoneDTO.class);
                        listZoneDTO3.add(zoneDTO3);
                    }

                    for (ZoneDTO zoneDTO3:listZoneDTO3) {
                        List<Zone> listZone4 = zoneService.getListByParentZoneCode(zoneDTO3.getZoneCode());
                        List<ZoneDTO> listZoneDTO4 = new ArrayList<ZoneDTO>();
                        for (Zone zone:listZone4) {
                            ZoneDTO zoneDTO4 = mapper.map(zone, ZoneDTO.class);
                            listZoneDTO4.add(zoneDTO4);
                        }

                        for (ZoneDTO zoneDTO4:listZoneDTO4) {
                            List<Zone> listZone5 = zoneService.getListByParentZoneCode(zoneDTO4.getZoneCode());
                            List<ZoneDTO> listZoneDTO5 = new ArrayList<ZoneDTO>();
                            for (Zone zone:listZone5) {
                                ZoneDTO zoneDTO5 = mapper.map(zone, ZoneDTO.class);
                                listZoneDTO5.add(zoneDTO5);
                            }


                            zoneDTO4.setListZoneDTO(listZoneDTO5);
                        }

                        zoneDTO3.setListZoneDTO(listZoneDTO4);
                    }

                    zoneDTO2.setListZoneDTO(listZoneDTO3);
                }

                zoneDTO.setListZoneDTO(listZoneDTO2);
            }

            return success(listZoneDTO);

        }catch (Exception e){
            e.printStackTrace();
            insertError("/Zone/ListForStaff",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //列表
    @RequestMapping(value = "ListForUser",method = RequestMethod.POST)
    public MyResult ListForUser(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String governmentCode = jsonObject.getString("governmentCode");
            String parentZoneCode = jsonObject.getString("parentZoneCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Zone/ListForUser");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<Zone> listZone = zoneService.getListByParentZoneCodeGovernmentCode(parentZoneCode,governmentCode);
            List<ZoneDTO> listZoneDTO = new ArrayList<ZoneDTO>();
            for (Zone zone:listZone) {
                ZoneDTO zoneDTO = mapper.map(zone, ZoneDTO.class);
                listZoneDTO.add(zoneDTO);
            }

            for (ZoneDTO zoneDTO:listZoneDTO) {
                List<Zone> listZone2 = zoneService.getListByParentZoneCode(zoneDTO.getZoneCode());
                List<ZoneDTO> listZoneDTO2 = new ArrayList<ZoneDTO>();
                for (Zone zone:listZone2) {
                    ZoneDTO zoneDTO2 = mapper.map(zone, ZoneDTO.class);
                    listZoneDTO2.add(zoneDTO2);
                }

                for (ZoneDTO zoneDTO2:listZoneDTO2) {
                    List<Zone> listZone3 = zoneService.getListByParentZoneCode(zoneDTO2.getZoneCode());
                    List<ZoneDTO> listZoneDTO3 = new ArrayList<ZoneDTO>();
                    for (Zone zone:listZone3) {
                        ZoneDTO zoneDTO3 = mapper.map(zone, ZoneDTO.class);
                        listZoneDTO3.add(zoneDTO3);
                    }

                    for (ZoneDTO zoneDTO3:listZoneDTO3) {
                        List<Zone> listZone4 = zoneService.getListByParentZoneCode(zoneDTO3.getZoneCode());
                        List<ZoneDTO> listZoneDTO4 = new ArrayList<ZoneDTO>();
                        for (Zone zone:listZone4) {
                            ZoneDTO zoneDTO4 = mapper.map(zone, ZoneDTO.class);
                            listZoneDTO4.add(zoneDTO4);
                        }

                        for (ZoneDTO zoneDTO4:listZoneDTO4) {
                            List<Zone> listZone5 = zoneService.getListByParentZoneCode(zoneDTO4.getZoneCode());
                            List<ZoneDTO> listZoneDTO5 = new ArrayList<ZoneDTO>();
                            for (Zone zone:listZone5) {
                                ZoneDTO zoneDTO5 = mapper.map(zone, ZoneDTO.class);
                                listZoneDTO5.add(zoneDTO5);
                            }


                            zoneDTO4.setListZoneDTO(listZoneDTO5);
                        }

                        zoneDTO3.setListZoneDTO(listZoneDTO4);
                    }

                    zoneDTO2.setListZoneDTO(listZoneDTO3);
                }

                zoneDTO.setListZoneDTO(listZoneDTO2);
            }


            return success(listZoneDTO);

        }catch (Exception e){
            insertError("/Zone/ListForUser",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //检查本级中是否存在该菜单
    Boolean exist(JSONArray arr, String zoneCode) {
        for (int k = 0; k < arr.size(); k++) {
            JSONObject menuDy = arr.getJSONObject(k);
            if (menuDy == null) {
                continue;
            }
            if (menuDy.getString("zoneCode").equals(zoneCode)) {
                return true;
            }
        }
        return false;
    }


    //添加菜单
    void AddZone(String parentCode,JSONArray arr,String governmentCode,int level) {


        //删除不存在的菜单
        List<Zone> listZoneTemp = zoneService.getListByParentZoneCodeGovernmentCode(parentCode,governmentCode);
        for (Zone zone: listZoneTemp) {
            if(!exist(arr,zone.getZoneCode())){
                zoneService.deleteByZoneCode(zone.getZoneCode());
            }
        }


        for (int k = 0; k < arr.size(); k++) {
            JSONObject menuDy = arr.getJSONObject(k);
            Zone zone = zoneService.getByZoneCode(menuDy.getString("zoneCode"));
            if (zone == null) {
                zone = new Zone();
                zone.setZoneCode(menuDy.getString("zoneCode"));
                zone.setParentZoneCode(menuDy.getString("parentZoneCode"));
                zone.setName(menuDy.getString("name").trim());
                zone.setSort(menuDy.getInteger("sort"));
                zone.setLevel(level);
                zone.setGovernmentCode(governmentCode);
                zoneService.insert(zone);
            } else {
                zone.setParentZoneCode(menuDy.getString("parentZoneCode"));
                zone.setName(menuDy.getString("name").trim());
                zone.setSort(menuDy.getInteger("sort"));
                zone.setLevel(menuDy.getInteger("level"));
                zone.setGovernmentCode(governmentCode);
                zoneService.updateByZoneCode(zone);
            }


            //添加子级菜单
            JSONArray listMenuDy = menuDy.getJSONArray("listZone");
            if (listMenuDy != null) {
                AddZone(menuDy.getString("zoneCode"),listMenuDy,governmentCode,level+1);
            }

        }

    }


    //编辑全部的菜单
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            JSONArray listZone = jsonObject.getJSONArray("listZone");


            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/Zone/Edit");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();


            if (listZone != null) {
                AddZone(emptyUUID,listZone,governmentCode,0);
            }


            Object data = new Object[] { };
            return success(data);

        }
        catch (Exception e) {
            insertError("/Zone/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //列表(for政府，组织)
    @RequestMapping(value = "ListToGovPlatform",method = RequestMethod.POST)
    public MyResult ListToGovPlatform(@RequestBody JSONObject jsonObject) {
        try{

            String governmentCode = jsonObject.getString("governmentCode");
            List<Zone> listZone = zoneService.getListByParentZoneCodeGovernmentCode(emptyUUID,governmentCode);
            List<ZoneDTO> listZoneDTO = new ArrayList<ZoneDTO>();
            for (Zone zone:listZone) {
                ZoneDTO zoneDTO = mapper.map(zone, ZoneDTO.class);
                listZoneDTO.add(zoneDTO);
            }

            for (ZoneDTO zoneDTO:listZoneDTO) {
                List<Zone> listZone2 = zoneService.getListByParentZoneCode(zoneDTO.getZoneCode());
                List<ZoneDTO> listZoneDTO2 = new ArrayList<ZoneDTO>();
                for (Zone zone:listZone2) {
                    ZoneDTO zoneDTO2 = mapper.map(zone, ZoneDTO.class);
                    listZoneDTO2.add(zoneDTO2);
                }

                for (ZoneDTO zoneDTO2:listZoneDTO2) {
                    List<Zone> listZone3 = zoneService.getListByParentZoneCode(zoneDTO2.getZoneCode());
                    List<ZoneDTO> listZoneDTO3 = new ArrayList<ZoneDTO>();
                    for (Zone zone:listZone3) {
                        ZoneDTO zoneDTO3 = mapper.map(zone, ZoneDTO.class);
                        listZoneDTO3.add(zoneDTO3);
                    }

                    for (ZoneDTO zoneDTO3:listZoneDTO3) {
                        List<Zone> listZone4 = zoneService.getListByParentZoneCode(zoneDTO3.getZoneCode());
                        List<ZoneDTO> listZoneDTO4 = new ArrayList<ZoneDTO>();
                        for (Zone zone:listZone4) {
                            ZoneDTO zoneDTO4 = mapper.map(zone, ZoneDTO.class);
                            listZoneDTO4.add(zoneDTO4);
                        }

                        for (ZoneDTO zoneDTO4:listZoneDTO4) {
                            List<Zone> listZone5 = zoneService.getListByParentZoneCode(zoneDTO4.getZoneCode());
                            List<ZoneDTO> listZoneDTO5 = new ArrayList<ZoneDTO>();
                            for (Zone zone:listZone5) {
                                ZoneDTO zoneDTO5 = mapper.map(zone, ZoneDTO.class);
                                listZoneDTO5.add(zoneDTO5);
                            }


                            zoneDTO4.setListZoneDTO(listZoneDTO5);
                        }

                        zoneDTO3.setListZoneDTO(listZoneDTO4);
                    }

                    zoneDTO2.setListZoneDTO(listZoneDTO3);
                }

                zoneDTO.setListZoneDTO(listZoneDTO2);
            }

            return success(listZoneDTO);

        }catch (Exception e){
            e.printStackTrace();
            insertError("/Zone/ListToGovPlatform",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



}
